package io.adrainty.algorithm.diagram.chapter1;

/**
 * <p>BinarySearch</p>
 *
 * @author AdRainty
 * @version V1.0.0
 * @description 二分查找算法
 * @since 2025/7/28 11:09:29
 */

public class BinarySearch {

    private BinarySearch() {}

    public static int binarySearch(int[] arr, int target) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            int guess = arr[mid];
            if (guess == target) return mid;
            else if (guess < target) left = mid + 1;
            else right = mid - 1;
        }
        return -1;
    }

}
